﻿Namespace Calendar
    Public Class _Default
        Inherits System.Web.UI.Page

        ' ----------------------------------------------------------------------------------------------------------------------
        ' Currently the following code is a rough version of a calendar to use.
        ' It still needs work to fit the purpose I have in mind.
        '
        ' I never used a DataList to display an event calendar but quite honestly for this project I want to stay away from 3rd
        ' party controls so I figured try something new see how it works. There is nothing saying using a DataList for the
        ' calendar will be my final choice here. I may switch to extending the stock calendar control instead.
        ' ----------------------------------------------------------------------------------------------------------------------
        ' TODO:
        ' ----------------------------------------------------------------------------------------------------------------------
        ' Retrieve the data from there instead of the temporary DataTable.
        '
        ' Database needs to contain:
        '   List of normal raid days.
        '
        ' The list of years ahead and back should be made dynamic not hard coded for only two years.
        ' Also this should forward the user to the current month they are on only a year ahead.
        '
        ' Layout is currently very generic and the calendar needs to be styled properly.
        '
        ' Event details and a sign up button should be placed in a light box type popup when an event is clicked on.
        '
        ' Events also need to be viewable or not depending on member rank.
        ' ----------------------------------------------------------------------------------------------------------------------

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim dDate As DateTime
                If Request("month") IsNot Nothing Then
                    dDate = Convert.ToDateTime(Server.UrlDecode(Request("month")))
                Else
                    dDate = DateTime.Now
                End If

                ' Due to the fact I have not yet layout the database for the event calendar I am for now going to use
                ' a datatable to supply temporary data for testing of the event calendar. After the database has been
                ' set up I will replace this datatable with a query to the database to retrieve data for the calendar.

                Dim dtEvents As New DataTable("DailyEvents")
                dtEvents.Columns.Add(New DataColumn("EventDate", System.Type.[GetType]("System.DateTime")))
                dtEvents.Columns.Add(New DataColumn("EventType"))
                dtEvents.Columns.Add(New DataColumn("EventTitle"))
                dtEvents.Columns.Add(New DataColumn("Closed"))
                dtEvents.Columns.Add(New DataColumn("Note"))

                dtEvents.Rows.Add(DateTime.Now, "Raid", "Icecrown Citidel (25 Normal)", "True", "Raid will begin at 7:30PM server time and end at 11:00PM server time.")
                dtEvents.Rows.Add(DateTime.Now, "Raid", "Icecrown Citidel (10 Normal)", "True", "Raid will begin at 3:00PM server time and end at 7:00PM server time.")
                dtEvents.Rows.Add(DateTime.Now.AddDays(5), "Raid", "Tournament of the Grand Crusader (25)", "True", "Raid will begin at 7:30PM server time and end at 11:00PM server time.")
                dtEvents.Rows.Add(DateTime.Now.AddMonths(1), "Raid", "Ulduar (25)", "True", "Raid will begin at 7:30PM server time and end at 11:00PM server time.")
                dtEvents.Rows.Add(DateTime.Now.AddMonths(1), "Raid", "Ulduar (10)", "True", "Raid will begin at 3:00PM server time and end at 7:00PM server time.")

                Dim dataTable As New DataTable
                dataTable.Columns.Add("Day")
                dataTable.Columns.Add("Data")

                Dim oRow As DataRow

                For i As Integer = 1 To DateTime.DaysInMonth(dDate.Year, dDate.Month)

                    Dim dCalendarDay As New DateTime(dDate.Year, dDate.Month, i)

                    ' Position the first day in the proper row in the calendar.
                    If i = 1 Then
                        Dim cnt As Integer = 0
                        Dim thisDay As String = dCalendarDay.ToString("ddd")
                        Select Case thisDay
                            Case "Mon"
                                oRow = dataTable.NewRow()
                                oRow("Data") = ""
                                dataTable.Rows.Add(oRow)
                            Case "Tue"
                                While Not cnt = 2
                                    cnt = cnt + 1
                                    oRow = dataTable.NewRow()
                                    oRow("Data") = ""
                                    dataTable.Rows.Add(oRow)
                                End While
                            Case "Wed"
                                While Not cnt = 3
                                    cnt = cnt + 1
                                    oRow = dataTable.NewRow()
                                    oRow("Data") = ""
                                    dataTable.Rows.Add(oRow)
                                End While
                            Case "Thu"
                                While Not cnt = 4
                                    cnt = cnt + 1
                                    oRow = dataTable.NewRow()
                                    oRow("Data") = ""
                                    dataTable.Rows.Add(oRow)
                                End While
                            Case "Fri"
                                While Not cnt = 5
                                    cnt = cnt + 1
                                    oRow = dataTable.NewRow()
                                    oRow("Data") = ""
                                    dataTable.Rows.Add(oRow)
                                End While
                            Case "Sat"
                                While Not cnt = 6
                                    cnt = cnt + 1
                                    oRow = dataTable.NewRow()
                                    oRow("Data") = ""
                                    dataTable.Rows.Add(oRow)
                                End While
                        End Select
                    End If

                    oRow = dataTable.NewRow()

                    ' Preparing data for a specific day. 
                    ' RaidDay method return: does the day is normal raid day or not. 
                    ' Business logic to change the color of this day needs to be added. 
                    ' GetEvents method: collects this day events from the data.

                    If Not RaidDay(dCalendarDay) Then
                        ' Display as normal day.
                        oRow("Data") = ((i.ToString() & " ") + dCalendarDay.ToString("ddd") & " ") + GetEvents(dCalendarDay, dtEvents)
                    Else
                        ' Display as normal raid day.
                        oRow("Data") = ((i.ToString() & " ") + dCalendarDay.ToString("ddd") & " ") + GetEvents(dCalendarDay, dtEvents)
                    End If

                    dataTable.Rows.Add(oRow)
                Next

                dlstCalendar.DataSource = dataTable
                dlstCalendar.DataBind()

                ' Make the current date block.
                If dDate.Year = DateTime.Now.Year AndAlso dDate.Month = DateTime.Now.Month Then
                    For Each oItem As DataListItem In dlstCalendar.Items
                        If oItem.ItemIndex = DateTime.Now.Day - 1 Then
                            oItem.BorderStyle = BorderStyle.Solid
                            oItem.BorderColor = System.Drawing.Color.DeepSkyBlue
                            oItem.BorderWidth = 2
                        End If
                    Next
                End If

            End If
        End Sub

        Public Function RaidDay(ByVal bDate As DateTime) As Boolean
            ' This sets Sunday as an off day.
            If bDate.ToString("ddd") = "Sun" Then
                Return True
            End If
            Return False
        End Function

        Public Function GetEvents(ByVal dDate As DateTime, ByVal dTable As DataTable) As String
            Dim str As String = ""
            For Each oItem As DataRow In dTable.Rows
                If Convert.ToDateTime(oItem("EventDate").ToString()).ToString("dd MMM, yyyy") = dDate.ToString("dd MMM, yyyy") Then
                    str = ((str & " ") + oItem("EventType") & ":") + oItem("EventTitle") & " "
                End If
            Next
            Return str
        End Function

        Protected Sub dlstCalendar_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
            Dim dDate As DateTime
            If Request("month") IsNot Nothing Then
                dDate = Convert.ToDateTime(Request("month"))
            Else
                dDate = DateTime.Now
            End If
            If e.Item.ItemType = ListItemType.Header Then
                Dim oPrev As DropDownList = DirectCast(e.Item.FindControl("ddlPrevious"), DropDownList)
                Dim oNext As DropDownList = DirectCast(e.Item.FindControl("ddlNext"), DropDownList)
                Dim dtYear As New DataTable()
                dtYear.Columns.Add("year4")
                dtYear.Columns.Add("sValue")
                ' This assumes that when user click on 2010 they want to see the January 2010 calendar.
                ' Should take the user to the same month they are on but a year ahead.
                dtYear.Rows.Add("2010", "01 Jan, 2010")
                dtYear.Rows.Add("2011", "01 Jan, 2011")
                oPrev.DataTextField = "year4"
                oPrev.DataValueField = "sValue"
                oPrev.DataSource = dtYear
                oPrev.DataBind()
                oNext.DataTextField = "year4"
                oNext.DataValueField = "sValue"
                oNext.DataSource = dtYear
                oNext.DataBind()

                DirectCast(e.Item.FindControl("lnkLeft"), HyperLink).NavigateUrl = "Calendar.aspx?month=" & Server.UrlEncode(dDate.AddMonths(-1).ToString("MMMM yyyy"))
                DirectCast(e.Item.FindControl("lblMiddle"), Label).Text = dDate.ToString("MMMM yyyy")
                DirectCast(e.Item.FindControl("lnkRight"), HyperLink).NavigateUrl = "Calendar.aspx?month=" & Server.UrlEncode(dDate.AddMonths(+1).ToString("MMMM yyyy"))

            End If
        End Sub

        Protected Sub RefreshData(ByVal sender As Object, ByVal e As System.EventArgs)
            ' Redirect the visitor by date so the page can render the calendar according to user selection.
            Response.Redirect("Calendar.aspx?month=" & DirectCast(sender, DropDownList).SelectedValue)
        End Sub

    End Class
End Namespace